home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / amc / amc_install.exe / {app} / Scripts / Culturalia+IMDB.ifs < prev    next >
Encoding:
Text File  |  2005-03-12  |  12.2 KB  |  404 lines

  1. (***************************************************
  2.  
  3. Ant Movie Catalog importation script
  4. www.antp.be/software/moviecatalog/
  5.  
  6. [Infos]
  7. Authors=David Arenillas, Antoine Potten and J.M. Folgueira
  8. Title=Culturalia(+IMDB)
  9. Description=Movie importation script for Culturalia and IMDB
  10. Site=http://www.culturalianet.com
  11. Language=ES
  12. Version=1.5 (05 Feb 2005)
  13. Requires=3.5.0
  14. Comments=Several updates made by: folgui (folgui@bigfoot.com), RedDwarf, Hades666, KaBeCi, PolloPolea, Moises DΘniz.||Thanks to Culturalia's webmaster for his help and for providing more direct access to his database.||
  15. License=The source code of the script can be used in another program only if full credits to script author and a link to Ant Movie Catalog website are given in the About box or in the documentation of the program.
  16. GetInfo=1
  17.  
  18. [Options]
  19. ImportLengthIMDB=0|0|0=Imports Length from Culturalia (no info)|1=Imports Length from IMDB
  20. ImportRatingIMDB=0|0|0=Imports Rating from Culturalia (no info)|1=Imports Rating from IMDB
  21. BatchMode=0|0|0=Normal working mode, prompts user when needed|1=Does not display any window, takes the first movie found
  22.  
  23. ***************************************************)
  24.  
  25. program Culturalia_IMDB;
  26. uses
  27.   StringUtils1;
  28. const
  29.   BaseURLCulturalia = 'http://www.culturalianet.com/bus/catalogo.php';
  30. var
  31.   MovieName, strTemp, donde: string;
  32.   Articles: array of string;
  33.   Index: integer;
  34.  
  35. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  36. var
  37.   i: Integer;
  38. begin
  39.   result := -1;
  40.   if StartAt < 0 then
  41.     StartAt := 0;
  42.   for i := StartAt to List.Count-1 do
  43.     if Pos(Pattern, List.GetString(i)) <> 0 then
  44.     begin
  45.       result := i;
  46.       Break;
  47.     end;
  48. end;
  49.  
  50. procedure AnalyzePageIMDB(Address: string);
  51. var
  52.   PageText: string;
  53.   Value: string;
  54. begin
  55.   PageText := GetPage(Address);
  56.   if pos('<title>IMDb', PageText) = 0 then
  57.   begin
  58.     AnalyzeMoviePageIMDB(PageText)
  59.   end else
  60.   begin
  61.     if Pos('<b>No Matches.</b>', PageText) > 0 then
  62.     begin
  63.       if GetOption('BatchMode') = 0 then
  64.         ShowMessage('No se ha encontrado ninguna coincidencia');
  65.       Exit;
  66.     end;
  67.     if GetOption('BatchMode') > 0 Then
  68.     begin
  69.       Value := TextBetween(PageText, '<ol><li>', '</li><li>');
  70.       Address := TextBetween(Value, '<a href="', '">');
  71.       AnalyzePageIMDB(Address);
  72.     end else
  73.     begin
  74.       PickTreeClear;
  75.       repeat
  76.         Value := TextBefore(PageText, '<ol>', '<b>');
  77.         if Value <> '' then
  78.         begin
  79.           HTMLRemoveTags(Value);
  80.           HTMLDecode(Value);
  81.           PickTreeAdd(Value, '');
  82.         end;
  83.         Value := TextBetween(PageText, '<ol>', '</ol>');
  84.         PageText := RemainingText;
  85.       until not AddMovieTitles(Value);
  86.       Value := TextBefore(PageText, '"><b>more titles</b></a>', '<a href="');
  87.       if Value <> '' then
  88.         PickTreeMoreLink('http://us.imdb.com' + Value);
  89.       if PickTreeExec(Address) then
  90.         AnalyzePageIMDB(Address);
  91.     end;
  92.   end;
  93. end;
  94.  
  95. procedure AnalyzeMoviePageIMDB(PageText: string);
  96. var
  97.   Value: string;
  98. begin
  99.   // Rating
  100.   if (GetOption('ImportRatingIMDB') = 1) then
  101.   begin
  102.    Value := TextBetween(PageText, '/rating-stars/', '/rating-vote/');
  103.    SetField(fieldRating, TextBetween(Value, '<b>', '/'));
  104.   end;
  105.  
  106.   // Length
  107.   if (GetOption('ImportLengthIMDB') = 1) then
  108.   begin
  109.     Value := TextBetween(PageText, '<b class="ch">Runtime:</b>' + #13#10, ' ');
  110.     if Value <> '' then
  111.     begin
  112.       if Pos(':', Value) > 0 then
  113.         SetField(fieldLength, TextAfter(Value, ':'))
  114.       else
  115.         SetField(fieldLength, Value);
  116.     end;
  117.   end;
  118. end;
  119.  
  120. function AddMovieTitles(List: string): Boolean;
  121. var
  122.   Value: string;
  123.   Address: string;
  124. begin
  125.   Result := False;
  126.   Value := TextBetween(List, '<li>', '</li>');
  127.   List := RemainingText;
  128.   while Value <> '' do
  129.   begin
  130.     Address := TextBetween(Value, '<a href="', '">');
  131.     HTMLRemoveTags(Value);
  132.     HTMLDecode(Value);
  133.     PickTreeAdd(Value, Address);
  134.     Result := True;
  135.     Value := TextBetween(List, '<li>', '</li>');
  136.     List := RemainingText;
  137.   end;
  138. end;
  139.  
  140. function TransformTitle(Title: string): string;
  141. var
  142.   BeginPos, EndPos: Integer;
  143.   Value: string;
  144.   Words: array of string;
  145.   Articles: array of string;
  146.   Replace,Original: string;
  147.   Index, CommaCount: Integer;
  148. Begin
  149.   // Original Title
  150.   Result:=Title;
  151.  
  152.   Setarraylength(Words,11);
  153.   Words[0]:=' In ';
  154.   Words[1]:=' On ';
  155.   Words[2]:=' Of ';
  156.   Words[3]:=' As ';
  157.   Words[4]:=' The ';
  158.   Words[5]:=' At ';
  159.   Words[6]:=' And A ';
  160.   Words[7]:=' And ';
  161.   Words[8]:=' An ';
  162.   Words[9]:=' To ';
  163.   Words[10]:=' For ';
  164.  
  165.   SetArrayLength(Articles,35);
  166.   Articles[0]:=' The';
  167.   Articles[1]:=' a';
  168.   Articles[2]:=' An';
  169.   Articles[3]:=' Le';
  170.   Articles[4]:=' L''';
  171.   Articles[5]:=' Les';
  172.   Articles[6]:=' Der';
  173.   Articles[7]:=' Das';
  174.   Articles[8]:=' Die';
  175.   Articles[9]:=' Des';
  176.   Articles[10]:=' Dem';
  177.   Articles[11]:=' Den';
  178.   Articles[12]:=' Ein';
  179.   Articles[13]:=' Eine';
  180.   Articles[14]:=' Einen';
  181.   Articles[15]:=' Einer';
  182.   Articles[16]:=' Eines';
  183.   Articles[17]:=' Einem';
  184.   Articles[18]:=' Il';
  185.   Articles[19]:=' Lo';
  186.   Articles[20]:=' La';
  187.   Articles[21]:=' I';
  188.   Articles[22]:=' Gli';
  189.   Articles[23]:=' Le';
  190.   Articles[24]:=' Uno';
  191.   Articles[25]:=' Una';
  192.   Articles[26]:=' Un''';
  193.   Articles[27]:=' O';
  194.   Articles[28]:=' Os';
  195.   Articles[29]:=' As';
  196.   Articles[30]:=' El';
  197.   Articles[31]:=' Los';
  198.   Articles[32]:=' Las';
  199.   Articles[33]:=' Unos';
  200.   Articles[34]:=' Unas';
  201.  
  202.   // Count the Comma in The Title
  203.   CommaCount := 0;
  204.   EndPos := 0;
  205.   Value := Title;
  206.   repeat
  207.      BeginPos := Pos(',', Value);
  208.      if BeginPos > 0 then
  209.      begin
  210.        Delete(Value, 1, BeginPos);
  211.        CommaCount := CommaCount + 1;
  212.        EndPos := EndPos + BeginPos;
  213.      end;
  214.   until( Pos(',',Value) = 0);
  215.  
  216.   // Compare the Article to a list of known ones
  217.   for Index := 0 to 34 do
  218.   begin
  219.     if Pos(Articles[Index], Value) <> 0 then
  220.     begin
  221.        CommaCount := 1;
  222.        BeginPos := EndPos;
  223.        Break;
  224.     end;
  225.   end;
  226.  
  227.   if (BeginPos > 0) and (CommaCount = 1) then
  228.   begin
  229.     Value := Copy(Title, BeginPos + 1, Length(Title));
  230.     Value := Trim(Value);
  231.     Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  232.   end;
  233.  
  234.   BeginPos := Pos(': ', Result);
  235.   if BeginPos > 0 then
  236.     Result := StringReplace(Result, ': ', ' - ');
  237.  
  238.   Result := AnsiMixedCase(Result, ' ');
  239.  
  240.   for Index := 0 to 10 do
  241.   begin
  242.     if Pos(Words[Index],Result) <> 0 then
  243.     begin
  244.       Original := Words[Index];
  245.       Replace := AnsiLowerCase(Original);
  246.       Result := StringReplace(Result, Original, Replace);
  247.     end;
  248.   end;
  249.  
  250.   Result := StringReplace(Result, ' - the ', ' - The ');
  251.   Result := Trim(Result);
  252. end;
  253.  
  254. procedure AnalyzePageCulturalia(Address: string);
  255. var
  256.   Page, TempTit: TStringList;
  257.   LineNr: Integer;
  258.   Code, Title, TitleOrig, Year: string;
  259.   TitleFound: Boolean;
  260. begin
  261.   Page := TStringList.Create;
  262.   TempTit := TStringList.Create;
  263.   Page.Text := GetPage(Address);
  264.   Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  265.   if Pos('No se ha encontrado ning·n artφculo por tφtulo', Page.Text) = 0 then
  266.   begin
  267.     if GetOption('BatchMode') = 0 then
  268.     begin
  269.        PickTreeClear;
  270.        LineNr := 1;
  271.        PickTreeAdd('Resultados mßs probables de la b·squeda:', '');
  272.        while LineNr + 3 < Page.Count do
  273.        begin
  274.          Code := TextAfter(Page.GetString(LineNr), 'Codigo = ');
  275.          Title := TextAfter(Page.GetString(LineNr+1), 'Titulo = ');
  276.          TitleOrig := TextAfter(Page.GetString(LineNr+2), 'Titulo original = ');
  277.          Year := TextAfter(Page.GetString(LineNr+3), 'A±o = ');
  278.          PickTreeAdd(Title + ' (' + TitleOrig + '), ' + Year, BaseURLCulturalia + '?catalogo=1&codigo=' + Code);
  279.          LineNr := LineNr + 5;
  280.        end;
  281.        Page.Free;
  282.        if PickTreeExec(Address) then
  283.          AnalyzeMoviePageCulturalia(Address);
  284.     end else
  285.     begin
  286.       LineNr := 1;
  287.       TitleFound := True;
  288.       Code := TextAfter(Page.GetString(LineNr), 'Codigo = ');
  289.       Address := (BaseURLCulturalia + '?catalogo=1&codigo=' + Code);
  290.       if TitleFound then
  291.         AnalyzeMoviePageCulturalia(Address);
  292.       Page.Free;
  293.     end;
  294.   end else
  295.   if (GetOption('BatchMode') = 0) then
  296.     ShowMessage('No se ha encontrado ninguna coincidencia por tφtulo');
  297. end;
  298.  
  299. procedure AnalyzeMoviePageCulturalia(Address: string);
  300. var
  301.   Page: TStringList;
  302.   Comments: string;
  303.   strTitle: string;
  304.   strSinopsis: string;
  305.   Line: string;
  306.   LineNr: Integer;
  307.   strTemp: string;
  308. begin
  309.   Page := TStringList.Create;
  310.   Page.Text := StringReplace(GetPage(Address), '<br><br>', #13#10);
  311.   Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  312.   strTitle := TextAfter(Page.GetString(1), 'Titulo = ');
  313.   if copy(strTitle, Length(strTitle), Length(strTitle)) = '.' then
  314.   begin
  315.     strTemp := Copy(strTitle, 1, Length(strTitle) -1);
  316.   end else
  317.   begin
  318.     strTemp := strTitle;
  319.   end;
  320.   SetField(fieldTranslatedTitle, TransformTitle(strTemp));
  321.   strTemp := TextAfter(Page.GetString(2), 'Titulo original = ');
  322.   SetField(fieldOriginalTitle, TransformTitle(strTemp));
  323.   SetField(fieldYear, TextAfter(Page.GetString(3), 'A±o = '));
  324.   SetField(fieldCategory, TextAfter(Page.GetString(4), 'Genero = '));
  325.   SetField(fieldCountry, TextAfter(Page.GetString(5), 'Nacion = '));
  326.   SetField(fieldDirector, TextAfter(Page.GetString(6), 'Director = '));
  327.   SetField(fieldActors, TextAfter(Page.GetString(7), 'Actores = '));
  328.   SetField(fieldProducer, TextAfter(Page.GetString(8), 'Productor = '));
  329.   Comments := 'Gui≤n: ' + TextAfter(Page.GetString(9), 'Guion = ');
  330.   Comments := Comments + #13#10 + 'Fotografφa: ' + TextAfter(Page.GetString(10), 'Fotografia = ');
  331.   Comments := Comments + #13#10 + 'M·sica: ' + TextAfter(Page.GetString(11), 'Musica = ');
  332.   SetField(fieldComments, Comments);
  333.   LineNr := FindLine('Sinopsis = ', Page, 0);
  334.   Line := Page.GetString(LineNr);
  335.   strSinopsis := TextAfter(Line, 'Sinopsis = ');
  336.   LineNr := LineNr + 1;
  337.   Line := Page.GetString(LineNr);
  338.   while pos('URL = ', Line) = 0 do
  339.   begin
  340.     strSinopsis := strSinopsis + #13#10 + Line;
  341.     LineNr := LineNr + 1;
  342.     Line := Page.GetString(LineNr);
  343.   end
  344.   HTMLRemoveTags(strSinopsis);
  345.   SetField(fieldDescription, StringReplace(StringReplace(strSinopsis, 'ô', '"'), 'ö', '"'));
  346.   LineNr := FindLine('URL = ', Page, 0);
  347.   if LineNr <> -1 then
  348.     SetField(fieldURL, TextAfter(Page.GetString(LineNr), 'URL = '));
  349.   LineNr := FindLine('Imagen = ', Page, 0);
  350.   if LineNr <> -1 then
  351.     GetPicture(TextAfter(Page.GetString(LineNr), 'Imagen = '));
  352.   Page.Free;
  353. end;
  354.  
  355. begin
  356.   SetArrayLength(Articles,11);
  357.   Articles[0]:='Lo ';
  358.   Articles[1]:='La ';
  359.   Articles[2]:='Le ';
  360.   Articles[3]:='Uno ';
  361.   Articles[4]:='Una ';
  362.   Articles[5]:='Un ';
  363.   Articles[6]:='El ';
  364.   Articles[7]:='Los ';
  365.   Articles[8]:='Las ';
  366.   Articles[9]:='Unos ';
  367.   Articles[10]:='Unas ';
  368.  
  369. if CheckVersion(3,5,0) then
  370.    begin
  371.     MovieName := '';
  372.     MovieName := GetField(fieldTranslatedTitle);
  373.      donde := '&donde=1';
  374.      if MovieName = '' then
  375.       begin
  376.        MovieName := GetField (fieldOriginalTitle);
  377.        donde := '&donde=2';
  378.       end
  379.      if MovieName = '' then
  380.       begin
  381.        Input('Importar de Culturalia', 'Introduzca el Titulo de la Pelicula:', MovieName);
  382.        donde := '&donde=1';
  383.       end
  384.     If MovieName <> '' then
  385.       begin
  386.         // Eliminate spanish article if exists
  387.         for Index := 0 to 10 do
  388.         begin
  389.          if Pos(Articles[Index], MovieName) <> 0 then
  390.          MovieName := copy(MovieName, length(Articles[Index]), length(MovieName));
  391.         end;
  392.  
  393.         // Eliminate point(s) at final of MovieName before search
  394.         strTemp := MovieName;
  395.         if Copy(strTemp, Length(strTemp), Length(strTemp)) = '.' then
  396.           MovieName := Copy(strTemp, 1, Length(strTemp) -1);
  397.         AnalyzePageCulturalia(BaseURLCulturalia + '?catalogo=1&texto=' + UrlEncode(MovieName) + donde);
  398.         if (GetOption('ImportRatingIMDB')=1) or (GetOption('ImportLengthIMDB')=1) then
  399.           AnalyzePageIMDB('http://imdb.com/find?more=tt;q='+UrlEncode(GetField(fieldOriginalTitle)));
  400.       end;
  401.    end else
  402.      ShowMessage('Este script requiere una versi≤n mßs reciente de Ant Movie Catalog (al menos la versi≤n 3.5.0)');
  403. end.
  404.